package utils

import (
	"crypto/md5"
	"encoding/hex"
	uuids "github.com/satori/go.uuid"
	"math/rand"
	"regexp"
	"strconv"
	"time"
)

// 获取当前时间
func GetTime() string {
	tn := time.Now()
	tf := tn.Format("2006-01-02 15:04:05")
	return tf
}

// 获取当前时间戳  默认返回int64类型
func GetTimeStamp() int64 {
	ts := time.Now().Unix()
	return ts
}

// 获取指定时间之前的时间戳 单位秒
func GetTimeStampOpt(num int64) int64 {
	ts := time.Now().Unix()
	tst := ts - (num)
	return tst
}

// 日期字符串转换成时间戳
func DateConversionStamp(data string) (int64, error) {
	timeTemplate := "2006-01-02 15:04:05"
	location, err := time.LoadLocation("Local")
	if err != nil {
		return 0, err
	}
	theTime, err := time.ParseInLocation(timeTemplate, data, location)
	if err != nil {
		return 0, nil
	}
	return theTime.Unix(), nil
}

// 时间戳转日期字符串
func StampConversionDate(stamp int64) string {
	timeTemplate := "2006-01-02 15:04:05"
	return time.Unix(stamp, 0).Format(timeTemplate)
}

// 验证日期
func VerifyDateFormat(date string) bool {
	regular := "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(date)

}

// 验证时间戳
func VerifyStampFormat(stamp int64) bool {
	regular := "^\\d{1,10}$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(strconv.FormatInt(stamp, 10))
}

// 验证邮箱格式
func VerifyEmailFormat(email string) bool {
	pattern := `^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z]\.){1,4}[a-z]{2,4}$`
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(email)
}

// 验证手机号格式
func VerifyMobileFormat(mobileNum string) bool {
	regular := "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(mobileNum)
}

// 验证姓名
func VerifyNameFormat(name string) bool {
	regular := "^([\u4e00-\u9fa5]+|([a-z]+\\s?)+)$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(name)
}

// 验证昵称
func VerifyNickFormat(nick string) bool {
	regular := "^[a-z][a-z0-9]{2,15}$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(nick)
}

// 验证密码(密码(以字母开头，长度在6~18之间，只能包含字母、数字和下划线))
func VerifyPasswordFormat(password string) bool {
	regular := "^[a-zA-Z]\\w{5,17}$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(password)
}

// 验证码验证
func VerifyAuthCodeFormat(authCode int) bool {
	regular := "^\\d{6}$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(strconv.Itoa(authCode))
}

// 验证性别
func VerifySexFormat(sex string) bool {
	regular := "^男$|^女$|^不确定$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(sex)
}

/*
获取32位置UUID
*/
func GetUUID() (uuid string) {
	u1 := uuids.NewV4()
	s1 := u1.String()
	return s1
}

/*
获取6位随机数
*/
func GetAuthCode() int {
	rand.Seed(time.Now().UnixNano())
	code := make([]int, 0)
	for i := 0; i < 9; i++ {
		baseCode := rand.Intn(1000000)
		if baseCode >= 100000 {
			code = append(code, baseCode)
		}
	}
	return code[rand.Intn(len(code))]
}

/*
MD5加密
*/
func MD5(str string) (md5String string, err error) {
	hasher := md5.New() // #nosec
	_, err = hasher.Write([]byte(str))
	if err != nil {
		return
	}
	cipherStr := hasher.Sum(nil)
	md5String = hex.EncodeToString(cipherStr)
	return
}
